﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AForge.Video.VFW;
using System.Threading;
using System.Drawing;

namespace VideoFlowStatistics.Class
{
    class AVIWriteClass
    {
        public static readonly AVIWriteClass AviWriteClass = new AVIWriteClass();

        private AVIWriter writer = null;
        private static DateTime StartTime = new DateTime();

        public AVIWriteClass()
        { }
        /// <summary>
        /// 写入视频
        /// </summary>
        /// <param name="motionLevel">当前视频变化值</param>
        /// <param name="image">视频帧</param>
        public void AVIWriteProcess(float motionLevel, Bitmap image)
        {
            //写视频
            if ((motionLevel != 0) && (Class.AppClass.IsSaveVideo != false))
            {
                if ((writer != null) && (DateTime.Compare(StartTime.AddMinutes(30), DateTime.Now) < 0))
                {
                    writer.Dispose();
                    writer = null;
                }
                if (writer == null)
                {
                    DateTime date = StartTime = DateTime.Now;
                    String fileName = String.Format("{0}-{1}-{2} {3}-{4}-{5}.avi",
                        date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);

                    try
                    {
                        // create AVI writer
                        writer = new AVIWriter("wmv3");
                        // open AVI file
                        int width = image.Width;
                        int height = image.Height;
                        if (width%2!=0)
                        {
                            width = width - width % 2;
                        }
                        if (height % 2 != 0)
                        {
                            height = height - height % 2;
                        }
                        writer.Open(fileName, width, height);
                    }
                    catch (ApplicationException ex)
                    {
                        if (writer != null)
                        {
                            writer.Dispose();
                            writer = null;
                        }
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                }

                Monitor.Enter(this);
                try
                {
                    writer.AddFrame(image);
                }
                catch (Exception ex)
                {
                    //if (ex.Message.IndexOf("Bitmap size must be of the same as video size") != -1)
                    //{
                    //    System.Windows.Forms.MessageBox.Show("多摄像头请设置相同分辨率");
                    //}
                    //else
                    //{
                    //    System.Windows.Forms.MessageBox.Show(ex.Message);
                    //}
                }
                
                Monitor.Exit(this);
            }
        }

        public void Display()
        {
            if (writer != null)
            {
                writer.Dispose();
                writer = null;
            }
        }
    }
}
